<?php
namespace backend\modules\admin\models;

use yii\base\Model;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use backend\modules\admin\models\Role;

class AdminForm extends Model
{
    public $username;
    public $password;
    public $realname;
    public $userid;
    public $roleid;
    public $lastloginip;
    public $lastlogintime;
    public $rememberme = true;
    private $_admin = false;

    //登陆
    public function login()
    {
        $this->_admin = Admin::findByUsername($this->username);
       
        if($this->_admin)
        {
            //echo $this->password.'<br>';
            //echo md5($this->password);
            //die();
            if($this->_admin->validatePassword(md5($this->password)))
            {
                \Yii::$app->session['adminuser']=$this->_admin;
                
                return true;
            }
            else
            {
             
                $this->_admin=null;
                return false;
            }
        }
        else
            return false;
    }

    public function rules(){
        return [
            [['username','password'],'required','message'=>'必填']
            ];
    }
    //注销
    public function logout()
    {
        $this->_admin = Admin::findByUsername($this->username);
        
        if($this->_admin)
        {
            $this->_admin=null;
            Yii::$app->session['adminuser']='';
          
            return true;
        }
        else
        {
            Yii::$app->session['adminuser']='';
          
        }
       
    }
    //获取当前管理员
    public function getAdmin()
    {
        if($this->_admin === false)
        {
            $this->_admin = Admin::findByUsername($this->username);
        }
        return $this->_admin;
    } 
    
}